package com.best.onegous.Adapter;


import java.util.ArrayList;
import java.util.List;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.best.onegous.R;
import com.best.onegous.agreement.ApiInterface;
import com.best.onegous.agreement.Apps;
import com.best.onegous.agreement.Common;
import com.best.onegous.details.PhotoActivity;
import com.best.onegous.details.User;
import com.best.onegous.http.HttpClients;
import com.best.onegous.viewutils.ToastView;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import android.content.Context;
import android.content.Intent;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class ShaiDanAdapter extends BaseAdapter{
	Intent intent;
	  private List<JSONObject> data = new ArrayList<JSONObject>();  
	    private LayoutInflater layoutInflater;  
	    private Context context;  
	    private Animation animation;
	    
	    public ShaiDanAdapter(Context context, List<JSONObject> data) {  
	        this.context = context;  
	        this.data = data;  
	        this.layoutInflater = LayoutInflater.from(context);  
	        animation = AnimationUtils.loadAnimation(context, R.anim.welcome_loading);
	    }     
	@Override
	public int getCount() {
		// TODO Auto-generated method stub

		return data.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub

		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}


	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		 ViewHome viewhome=null;
			if(convertView==null){
				viewhome = new ViewHome();
				convertView = layoutInflater.inflate(R.layout.shaidan_item, null);
				viewhome.userimg = (ImageView) convertView.findViewById(R.id.userimg);
				viewhome.dianzan = (LinearLayout) convertView.findViewById(R.id.dianzan);
				viewhome.allimg = (LinearLayout) convertView.findViewById(R.id.allimg);
				viewhome.want = (TextView) convertView.findViewById(R.id.want);
				
				viewhome.username = (TextView) convertView.findViewById(R.id.username);
				viewhome.userid = (TextView) convertView.findViewById(R.id.userid);
				viewhome.time = (TextView) convertView.findViewById(R.id.time);
				viewhome.title = (TextView) convertView.findViewById(R.id.title);
				viewhome.context = (TextView) convertView.findViewById(R.id.context);
				viewhome.dizhi = (TextView) convertView.findViewById(R.id.dizhi);
				
				viewhome.zan = (ImageView) convertView.findViewById(R.id.zan);
				viewhome.img1 = (ImageView) convertView.findViewById(R.id.img1);
				viewhome.img2 = (ImageView) convertView.findViewById(R.id.img2);
				viewhome.img3 = (ImageView) convertView.findViewById(R.id.img3);
				convertView.setTag(viewhome);
			}else{
				viewhome = (ViewHome) convertView.getTag();
			}
			JSONObject item = data.get(position);
			try {
				JSONArray postAllPic = item.getJSONArray("sd_photolist");
				int size = postAllPic.length();
				
		
				if(size>0){
					viewhome.allimg.setVisibility(View.VISIBLE);
					ArrayList<String> imglist = new ArrayList<String>();
					for(int i = 0;i<size;i++){
						String str = postAllPic.get(i).toString();
						imglist.add(str);
						switch (i) {
						case 0:
							viewhome.img1.setVisibility(View.VISIBLE);
							Apps.imageLoader.displayImage(Common.PHOTOURL+str, viewhome.img1, Apps.yuanimg);
							viewhome.img1.setOnClickListener(new ImgItemListener(imglist,"0"));
							break;
						case 1:
							viewhome.img2.setVisibility(View.VISIBLE);
							Apps.imageLoader.displayImage(Common.PHOTOURL+str, viewhome.img2, Apps.yuanimg);
							viewhome.img2.setOnClickListener(new ImgItemListener(imglist,"1"));
							break;
						case 2:
							viewhome.img3.setVisibility(View.VISIBLE);
							Apps.imageLoader.displayImage(Common.PHOTOURL+str, viewhome.img3, Apps.yuanimg);
							viewhome.img3.setOnClickListener(new ImgItemListener(imglist,"2"));
							break;
						}
						
					}
				}
				
				Apps.imageLoader.displayImage(Common.PHOTOURL+item.getString("userphoto"), viewhome.userimg, Apps.touxiang);
				viewhome.username.setText(item.getString("username"));
				viewhome.userid.setText("ID："+item.getString("sd_userid"));
				viewhome.time.setText(item.getString("sd_time"));
				viewhome.title.setText(item.getString("shop_title"));
				viewhome.context.setText(item.getString("sd_content"));
				viewhome.dizhi.setText(item.getString("sd_ip"));
				
				
				viewhome.userimg.setOnClickListener(new OnClickListener() {
					 
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
					
						intent = new Intent(context,User.class);
						context.startActivity(intent);
					}
				});
				viewhome.dianzan.setOnClickListener(new ToolsItemListener(viewhome.zan,item.getString("sd_id")));
				
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			return convertView;
		}
	
	private class ImgItemListener implements View.OnClickListener {
		private String sum;
		private ArrayList<String> imglist;
		public ImgItemListener(ArrayList<String> imglist,String sum) {
			this.imglist = imglist;
			this.sum = sum;
		}
		@Override
		public void onClick(View v) {
				intent = new Intent(context,PhotoActivity.class);
				intent.putExtra("img", imglist);
				intent.putExtra("sum", sum);
				context.startActivity(intent);
		
			
		}
	}
	
	
	private class ToolsItemListener implements View.OnClickListener {
		private ImageView img;
		private String id;
		private ToastView toast;
		public ToolsItemListener(ImageView img,String id) {
			this.img = img;
			this.id = id;
		}
		@Override
		public void onClick(View v) {
			img.startAnimation(animation);

			RequestParams params = new RequestParams();
			  params.add("id",id);
			new AsyncHttpClient().post(ApiInterface.DIANZAN, params, new AsyncHttpResponseHandler() {
				@Override
				public void onSuccess(int statusCode, Header[] headers,
						byte[] responseBody) {
					try {
						JSONObject jo = new JSONObject(new String(responseBody));
						if("1".equals(jo.getString("status"))){
							toast = new ToastView(context	, "点赞成功！");
					      	toast.setGravity(Gravity.CENTER, 0, 0);
					          toast.show();
						}else{
							toast = new ToastView(context	, "点赞失败！");
					      	toast.setGravity(Gravity.CENTER, 0, 0);
					          toast.show();
						}
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}

				@Override
				public void onFailure(int statusCode, Header[] headers,
						byte[] responseBody, Throwable error) {
					toast = new ToastView(context	, "网络不给力！");
			      	toast.setGravity(Gravity.CENTER, 0, 0);
			          toast.show();
					
				}
			});
		}
	}
	 class ViewHome{  
		 private ImageView userimg,img1,img2,img3,zan;
		 private LinearLayout dianzan,allimg;
		    private TextView username,userid,time,title,context,dizhi,want;
		    
		}
		
	}